﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Components;
using Model;


namespace MsDAL
{
   public partial  class Sto7a00Base
    {		
		/// <summary>
        /// 增加一条数据
        /// </summary>
        public virtual  SqlApp Add(Model.Sto7a00 sto7a00)
        {
			StringBuilder strSql = new StringBuilder();
			strSql.Append("insert into Sto7a00(");
			strSql.Append("trandocno ,outstk ,instk ,muser ,senduser ,recuser ,moduser ,recaddress ,mdate ,msdate ,mrdate ,sendate ,recdate ,trancom ,usept ,status ,tranamtotal ,tranqtytotal ,sendamtotal ,sendqtytotal ,recamtotal ,recqtytotal ,remark1 ,remark2 ,tranid ,usercode )");
			strSql.Append(" values (");
			strSql.Append("@trandocno ,@outstk ,@instk ,@muser ,@senduser ,@recuser ,@moduser ,@recaddress ,@mdate ,@msdate ,@mrdate ,@sendate ,@recdate ,@trancom ,@usept ,@status ,@tranamtotal ,@tranqtytotal ,@sendamtotal ,@sendqtytotal ,@recamtotal ,@recqtytotal ,@remark1 ,@remark2 ,@tranid ,@usercode )");		

			SqlParameter[] parameters = {
				new SqlParameter("@trandocno",SqlDbType.NVarChar),
				new SqlParameter("@outstk",SqlDbType.NVarChar),
				new SqlParameter("@instk",SqlDbType.NVarChar),
				new SqlParameter("@muser",SqlDbType.NVarChar),
				new SqlParameter("@senduser",SqlDbType.NVarChar),
				new SqlParameter("@recuser",SqlDbType.NVarChar),
				new SqlParameter("@moduser",SqlDbType.NVarChar),
				new SqlParameter("@recaddress",SqlDbType.NVarChar),
				new SqlParameter("@mdate",SqlDbType.DateTime),
				new SqlParameter("@msdate",SqlDbType.DateTime),
				new SqlParameter("@mrdate",SqlDbType.DateTime),
				new SqlParameter("@sendate",SqlDbType.DateTime),
				new SqlParameter("@recdate",SqlDbType.DateTime),
				new SqlParameter("@trancom",SqlDbType.NVarChar),
				new SqlParameter("@usept",SqlDbType.Decimal),
				new SqlParameter("@status",SqlDbType.Int),
				new SqlParameter("@tranamtotal",SqlDbType.Decimal),
				new SqlParameter("@tranqtytotal",SqlDbType.Decimal),
				new SqlParameter("@sendamtotal",SqlDbType.Decimal),
				new SqlParameter("@sendqtytotal",SqlDbType.Decimal),
				new SqlParameter("@recamtotal",SqlDbType.Decimal),
				new SqlParameter("@recqtytotal",SqlDbType.Decimal),
				new SqlParameter("@remark1",SqlDbType.NVarChar),
				new SqlParameter("@remark2",SqlDbType.NVarChar),
				new SqlParameter("@tranid",SqlDbType.NVarChar),
				new SqlParameter("@usercode",SqlDbType.NVarChar)
				};
			parameters[0].Value = sto7a00.Trandocno;
			parameters[1].Value = sto7a00.Outstk;
			parameters[2].Value = sto7a00.Instk;
			parameters[3].Value = sto7a00.Muser;
			parameters[4].Value = sto7a00.Senduser;
			parameters[5].Value = sto7a00.Recuser;
			parameters[6].Value = sto7a00.Moduser;
			parameters[7].Value = sto7a00.Recaddress;
			parameters[8].Value = sto7a00.Mdate;
			parameters[9].Value = sto7a00.Msdate;
			parameters[10].Value = sto7a00.Mrdate;
			parameters[11].Value = sto7a00.Sendate;
			parameters[12].Value = sto7a00.Recdate;
			parameters[13].Value = sto7a00.Trancom;
			parameters[14].Value = sto7a00.Usept;
			parameters[15].Value = sto7a00.Status;
			parameters[16].Value = sto7a00.Tranamtotal;
			parameters[17].Value = sto7a00.Tranqtytotal;
			parameters[18].Value = sto7a00.Sendamtotal;
			parameters[19].Value = sto7a00.Sendqtytotal;
			parameters[20].Value = sto7a00.Recamtotal;
			parameters[21].Value = sto7a00.Recqtytotal;
			parameters[22].Value = sto7a00.Remark1;
			parameters[23].Value = sto7a00.Remark2;
			parameters[24].Value = sto7a00.Tranid;
			parameters[25].Value = sto7a00.Usercode;
			
			return new SqlApp(strSql.ToString(),new ArrayList(parameters), false, "数据保存失败！");
        }
	
        /// <summary>
        /// 判断某个值是否存在表中的某个字段
        /// </summary>
        /// <param name="Condition">条件字段名 例:A=@A</param>
        /// <param name="hs">参数和值 如hs["@A"]=123</param>
        /// <returns></returns>
        public virtual bool Exists(String condition, Hashtable hs)
        {
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select count(1) from Sto7a00 ");
			if (!string.IsNullOrEmpty(condition))
				strSql.Append(" where " + condition);
			List<SqlParameter> list = DBHelper.GetListByHashtable(hs);
			if (list.Count > 0)
				return Convert.ToInt32(DBHelper.ExecuteScalar(strSql.ToString(), list.ToArray())) > 0;
			else
				return Convert.ToInt32(DBHelper.ExecuteScalar(strSql.ToString())) > 0;
        }

		/// <summary>
        /// 更新一条数据
        /// </summary>
        public virtual SqlApp Update(Model.Sto7a00 sto7a00)
        {
			StringBuilder strSql = new StringBuilder();
			strSql.Append("update Sto7a00 set ");
			strSql.Append("trandocno=@trandocno ,outstk=@outstk ,instk=@instk ,muser=@muser ,senduser=@senduser ,recuser=@recuser ,moduser=@moduser ,recaddress=@recaddress ,mdate=@mdate ,msdate=@msdate ,mrdate=@mrdate ,sendate=@sendate ,recdate=@recdate ,trancom=@trancom ,usept=@usept ,status=@status ,tranamtotal=@tranamtotal ,tranqtytotal=@tranqtytotal ,sendamtotal=@sendamtotal ,sendqtytotal=@sendqtytotal ,recamtotal=@recamtotal ,recqtytotal=@recqtytotal ,remark1=@remark1 ,remark2=@remark2 ,usercode=@usercode ");
			strSql.Append("where tranid=@tranid ");
			SqlParameter[] parameters = {
				new SqlParameter("@trandocno", SqlDbType.NVarChar),
				new SqlParameter("@outstk", SqlDbType.NVarChar),
				new SqlParameter("@instk", SqlDbType.NVarChar),
				new SqlParameter("@muser", SqlDbType.NVarChar),
				new SqlParameter("@senduser", SqlDbType.NVarChar),
				new SqlParameter("@recuser", SqlDbType.NVarChar),
				new SqlParameter("@moduser", SqlDbType.NVarChar),
				new SqlParameter("@recaddress", SqlDbType.NVarChar),
				new SqlParameter("@mdate", SqlDbType.DateTime),
				new SqlParameter("@msdate", SqlDbType.DateTime),
				new SqlParameter("@mrdate", SqlDbType.DateTime),
				new SqlParameter("@sendate", SqlDbType.DateTime),
				new SqlParameter("@recdate", SqlDbType.DateTime),
				new SqlParameter("@trancom", SqlDbType.NVarChar),
				new SqlParameter("@usept", SqlDbType.Decimal),
				new SqlParameter("@status", SqlDbType.Int),
				new SqlParameter("@tranamtotal", SqlDbType.Decimal),
				new SqlParameter("@tranqtytotal", SqlDbType.Decimal),
				new SqlParameter("@sendamtotal", SqlDbType.Decimal),
				new SqlParameter("@sendqtytotal", SqlDbType.Decimal),
				new SqlParameter("@recamtotal", SqlDbType.Decimal),
				new SqlParameter("@recqtytotal", SqlDbType.Decimal),
				new SqlParameter("@remark1", SqlDbType.NVarChar),
				new SqlParameter("@remark2", SqlDbType.NVarChar),
				new SqlParameter("@tranid", SqlDbType.NVarChar),
				new SqlParameter("@usercode", SqlDbType.NVarChar)
				};
			parameters[0].Value = sto7a00.Trandocno;
			parameters[1].Value = sto7a00.Outstk;
			parameters[2].Value = sto7a00.Instk;
			parameters[3].Value = sto7a00.Muser;
			parameters[4].Value = sto7a00.Senduser;
			parameters[5].Value = sto7a00.Recuser;
			parameters[6].Value = sto7a00.Moduser;
			parameters[7].Value = sto7a00.Recaddress;
			parameters[8].Value = sto7a00.Mdate;
			parameters[9].Value = sto7a00.Msdate;
			parameters[10].Value = sto7a00.Mrdate;
			parameters[11].Value = sto7a00.Sendate;
			parameters[12].Value = sto7a00.Recdate;
			parameters[13].Value = sto7a00.Trancom;
			parameters[14].Value = sto7a00.Usept;
			parameters[15].Value = sto7a00.Status;
			parameters[16].Value = sto7a00.Tranamtotal;
			parameters[17].Value = sto7a00.Tranqtytotal;
			parameters[18].Value = sto7a00.Sendamtotal;
			parameters[19].Value = sto7a00.Sendqtytotal;
			parameters[20].Value = sto7a00.Recamtotal;
			parameters[21].Value = sto7a00.Recqtytotal;
			parameters[22].Value = sto7a00.Remark1;
			parameters[23].Value = sto7a00.Remark2;
			parameters[24].Value = sto7a00.Tranid;
			parameters[25].Value = sto7a00.Usercode;
	
			return new SqlApp(strSql.ToString(),new ArrayList(parameters), false, "数据保存失败！");			
		}
		
		public virtual SqlApp Delete(Model.Sto7a00 sto7a00)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("DELETE FROM  Sto7a00  ");
			strSql.Append("WHERE tranid=@tranid ");
			SqlParameter[] parameters = {
				new SqlParameter("@tranid", SqlDbType.NVarChar)
				};
			parameters[0].Value = sto7a00.Tranid;
	
			return new SqlApp(strSql.ToString(),new ArrayList(parameters), false, "数据保存失败！");		
		}
		
		/// <summary>
        /// 根据Tranid得到整个 Sto7a00对象,如果没查到，返回null
        /// </summary>
        /// <param name="Tranid">该实体主键 Tranid</param>
        /// <returns>实体Sto7a00的对象</returns>
		public virtual Model.Sto7a00 GetModelById(string Tranid)
        {
			string where = "tranid=@tranid ";
			
			string sql = GetSql(where,null);
			
			List<SqlParameter> parameters = new List<SqlParameter>();
			
			SqlParameter p0 = new SqlParameter("@Tranid",SqlDbType.NVarChar);
			p0.Value = Tranid;
			parameters.Add(p0);
			
			DataTable dt = DBHelper.GetData(sql, parameters.ToArray());
			
			//调用实体Sto7a00的GetSto7a00ListByDataTable方法得到实体对象
			List<Model.Sto7a00> list =Model.Sto7a00.GetSto7a00ListByDataTable(dt);
			
			return  (list.Count>0) ? list[0] : null;
        }
		
		
		 /// <summary>
        /// 得到Sql语句
        /// </summary>
        /// <param name="Condition">Sql语句的条件</param>
		/// <param name="OrderBy">排序条件，默认为String.Empty</param>
        /// <returns></returns>
        protected virtual String GetSql(string condition,string orderBy)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [trandocno] ,[outstk] ,[instk] ,[muser] ,[senduser] ,[recuser] ,[moduser] ,[recaddress] ,[mdate] ,[msdate] ,[mrdate] ,[sendate] ,[recdate] ,[trancom] ,[usept] ,[status] ,[tranamtotal] ,[tranqtytotal] ,[sendamtotal] ,[sendqtytotal] ,[recamtotal] ,[recqtytotal] ,[remark1] ,[remark2] ,[tranid] ,[usercode] ");
            strSql.Append(" FROM Sto7a00 ");
 			
			if (!string.IsNullOrEmpty(condition)) strSql.Append(" where " + condition);				
			if(!string.IsNullOrEmpty(orderBy)) strSql.Append( " order by " + orderBy );
			
            return  strSql.ToString();
        }

        /// <summary>
        /// 根据条件和参数得到Sto7a00对象
        /// </summary>
        /// <param name="Condition">Sql条件</param>
        /// <param name="hs">参数和值</param>
        /// <returns></returns>
        public virtual Model.Sto7a00 GetModel(String Condition, Hashtable hs)
        {
			String sql = GetSql(Condition,String.Empty);
			DataTable dt = DBHelper.GetTableByCondition(sql,  hs);
			return (dt.Rows.Count > 0) ? Model.Sto7a00.GetSto7a00ListByDataTable(dt)[0] : null;
        }

        /// <summary>
        /// 根据主键的值和某字段得到其值,可以用来判断是否存在
        /// </summary>
        /// <param name="TableKeyValue">主键值</param>
        /// <param name="FieldName">要查询的字段名称</param>
        /// <returns></returns>
        public virtual Object GetValueByField(Object TableKeyValue, Object FieldName)
        {
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select " + FieldName + " from Sto7a00 ");
			strSql.Append(" where tranid=@TableKeyValue ");
			SqlParameter p = new SqlParameter("@TableKeyValue", TableKeyValue);

			return DBHelper.ExecuteScalar(strSql.ToString(), p);
        }
		
        /// <summary>
        /// 分页SQL
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual ReturnPageData GetList(Pager page)
        {
            ReturnPageData data = new ReturnPageData();
            //page.PageIndex++;
            page.PrimaryKey = "tranid ";

            page.ShowFields = " [trandocno] ,[outstk] ,[instk] ,[muser] ,[senduser] ,[recuser] ,[moduser] ,[recaddress] ,[mdate] ,[msdate] ,[mrdate] ,[sendate] ,[recdate] ,[trancom] ,[usept] ,[status] ,[tranamtotal] ,[tranqtytotal] ,[sendamtotal] ,[sendqtytotal] ,[recamtotal] ,[recqtytotal] ,[remark1] ,[remark2] ,[tranid] ,[usercode] ";

            page.TableName = " Sto7a00 ";

			page.StrWhere = string.IsNullOrEmpty(page.StrWhere) ? " 1=1 " : page.StrWhere;
			
            page.StrWhere = string.IsNullOrEmpty(page.AutoWhere) ? page.StrWhere : page.StrWhere + " And ("+ page.AutoWhere +")";

            page.For = PagerFor.Count;
            DataTable dtRowCount = DBHelper.GetSelectLimit(page);
            if (dtRowCount == null || dtRowCount.Rows.Count == 0)
            {
                data.AllRowCount = 0;
                return data; //NULL DATA
            }
            data.AllRowCount = Convert.ToInt32(dtRowCount.Rows[0]["Total"]);
            page.For = PagerFor.Data;

            DataTable dtPageData = DBHelper.GetSelectLimit(page);
            data.CurrnetPageData = dtPageData;

            //汇总
            page.For = PagerFor.Summary;
            data.SummaryData = DBHelper.GetSelectLimit(page);
            
            return data;
        }		
		
		public virtual DataTable GetList(string c,Hashtable hs)
		{
			string sql = GetSql(c,null); //得到SQL语句
			//返回结果集
			return DBHelper.GetData(sql,DBHelper.GetListByHashtable(hs).ToArray());
		}
    }
}
